/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.edu.unifei.portal.dao;

import br.edu.unifei.portal.modelo.Curso;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;

/**
 *
 * @author Douglas Soares
 */
public class CursoDao {

    private EntityManager em;

    public CursoDao(EntityManager em) {
        this.em = em;
    }

    public void create(Curso curso) {
        em.getTransaction().begin();
        em.persist(curso);
        em.getTransaction().commit();
    }

    public void update(Curso curso) {
        em.getTransaction().begin();
        em.merge(curso);
        em.getTransaction().commit();
    }

    public void delete(String nome) {
        em.getTransaction().begin();
        Curso c = findCurso(nome);
        em.remove(c);
        em.getTransaction().commit();
    }

    public Curso findCurso(String nome) {
        return (Curso) em.createQuery(
                "from Curso where nome = '" + nome + "'").getSingleResult();
    }

    public List<Curso> findAllCurso() {
        return em.createQuery("from Curso").getResultList();
    }

    public List<Curso> findPageCurso(int max, int first) {
        Query query = em.createQuery("from Curso");
        query.setMaxResults(max);
        query.setFirstResult(first);
        return query.getResultList();
    }

    public long getCountCurso() {
        Query query = em.createQuery("from Curso");
        return (Long) em.createQuery(
                "select count(*) from Curso").getSingleResult();
    }
}
